package com.peza.homecontrol.domain;

import java.io.IOException;

import javax.persistence.Column;
import javax.persistence.Entity;

import com.peza.homecontrol.services.EventService;

@Entity
public class Switch{

	@Column
	private long id;
	
	@Column
	private Module module;

	@Column
	private Action action;
	
	
	private EventService dao;
	
	private static final String EXEC = "KAKUcli.exe";
	
	private static final String PATH = "C:/Java/Eclipse_workspace/homecontrol/src/main/resources/bin/";
	
	
	
	public Switch() {
		
	}

	public Switch(Module module, Action action) {
		this.module = module;
		this.action = action;
	}

	
	public void execute(){
		
		String command = generateCommand();
		System.out.println("executing command : " + command);
		try {
			Process p = Runtime.getRuntime().exec(command);
			module.flip();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		 
	}
	
	public Module getModule() {
		return module;
	}

	public void setModule(Module module) {
		this.module = module;
	}

	public Action getAction() {
		return action;
	}

	public void setAction(Action action) {
		this.action = action;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	
	private String generateCommand(){
		
		String activity = "";
		
		switch(this.action){
		case ON:
			activity = "aan";
			break;
		
		case OFF:
			activity = "uit";
			break;
		
		case DIM:
			activity = Integer.toString(action.getSetting());
			break;
			
		default:
			break;
		
		}
		return PATH+EXEC+" " + module.getCode() + " "+activity;
		
		
	}
	
	
}
